﻿using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Itech_Run.HttpApi.Host.Filter
{
    public class SwaggerDocSchemaFilter : IDocumentFilter
    {
        private static readonly string[] IgnorePaths = { "/Abp" };
        private static readonly string[] IgnoreSchema = { "Volo.Abp" };

        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            //Rmove API
            var pathToRemove = swaggerDoc.Paths
                .Where(pathItem => IgnorePaths.Any(x => pathItem.Key.Contains(x, StringComparison.OrdinalIgnoreCase)))
                .ToList();

            foreach (var path in pathToRemove)
            {
                swaggerDoc.Paths.Remove(path.Key);
            }

            ////Remove Default Response Examples
            //foreach (var item in swaggerDoc.Paths)
            //{
            //    item.Value.Operations.Values.First().Responses.RemoveAll(p => true);
            //}

           //Remove Schema
           var schemaToRemove =
                swaggerDoc.Components.Schemas.Where(sm => IgnoreSchema.Any(x => sm.Key.Contains(x, StringComparison.OrdinalIgnoreCase))).ToList();
            foreach (var schema in schemaToRemove)
            {
                swaggerDoc.Components.Schemas.Remove(schema.Key);
            }

        }
    }
}
